const fs = require('fs');
const path = require('path');

const logDir = path.join(__dirname, '../logs');
const logFilePath = path.join(logDir, 'server.log');

// 创建logs目录，如果不存在  
if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const logStream = fs.createWriteStream(logFilePath, { flags: 'a' });

const log = (type, message) => {
    const timestamp = new Date().toISOString();
    const logMessage = `[${type.toUpperCase()}] ${timestamp}: ${message}`;

    // 写入文件  
    logStream.write(logMessage + '\n');

    // 输出到控制台  
    console.log(logMessage);
};

module.exports = { log };